<script>
import { messageType } from '@/constant'
import { sendMessageAPI } from '@/api/employeeApi'

export default {
  name: 'messageMuch',
  data() {
    return {
      /* 消息分类 */
      messageType,
      /* 发送给服务器的参数 */
      sendMessageParams: {
        // 要群发的用户的id数组 [1,2,3,4,5]
        userIds: [],
        // 消息类型 1 通知消息 2 提示消息 3 重要消息 4 紧急消息
        type: '',
        // 群发的消息内容，5-100个字符
        content: ''
      },
      /* 规则对象 */
      formRules: {
        type: [{ required: true, message: '请选择消息类型', trigger: 'change' }],
        content: [{ required: true, message: '请输入消息内容', trigger: 'blur' }]
      }
    }
  },
  props: {
    /* 用于显示和隐藏弹框 */
    showMessageMuch: {
      type: Boolean,
      default: false
    },
    /* 父组件选中的员工列表 */
    selectEmployee: {
      type: Array,
      default: () => ([])
    }
  },
  methods: {
    /* 弹框关闭时的回调函数 */
    handleClose() {
      this.$emit('update:showMessageMuch', false)
    },
    /* 点击取消发送按钮的回调函数 */
    handleCancel() {
      this.$emit('update:showMessageMuch', false)
    },
    /* 删除一个tag标签的回调函数 */
    clickTag(index) {
      this.selectEmployee.splice(index, 1)
    },
    /* 点击发送消息按钮的回调函数 */
    async handleSendMessage() {
      const valida = await this.$refs.form.validate().catch(e => e)
      if (!valida) return
      try {
        /* 整理参数 */
        const userIds = this.selectEmployee.map(item => item.id)
        const { type, content } = this.sendMessageParams
        //  调用sendMessageAPI()接口函数传入整理好的参数
        const { message } = await sendMessageAPI({ userIds, type, content })
        this.$message.success(message)
        // 通知父组件关闭弹框
        this.$emit('sendSuccess')
      } catch (e) {
        this.$message.error(e.message)
      }
    }
  },
  watch: {
    /* 监听 showMessageMuch字段的变化重置表单数据 */
    showMessageMuch(newValue, oldValue) {
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    }
  }
}
</script>

<template>
  <el-dialog title="群发通知" width="40%" :visible="showMessageMuch" @close="handleClose">
    <el-form label-width="120px" ref="form" :model="sendMessageParams" :rules="formRules">
      <el-form-item label="群发的员工">
        <el-tag v-for="(tag,index) in selectEmployee" :key="tag.id" effect="dark" closable @close="clickTag(index)">@ {{ tag.username }}</el-tag>
      </el-form-item>
      <el-form-item label="消息分类" prop="type">
        <el-select placeholder="选择消息类型" v-model="sendMessageParams.type">
          <el-option v-for="(item,index) in messageType" :label="item.label" :value="item.type" :key="item.type"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="通知内容" prop="content">
        <el-input type="textarea" resize="none" rows="4" v-model="sendMessageParams.content"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="handleCancel">取消发送</el-button>
    <el-button type="primary" @click="handleSendMessage">发送通知</el-button>
  </span>
  </el-dialog>
</template>

<style scoped lang="scss">
.el-tag {
  margin: 0 5px;
}
</style>
